<?php
namespace app\api\model;


use app\model\Base;

class UserWithdraw extends Base
{
    const TRANSFER_SN_PREFIX = 'TSF';
    const WITHDRAW_SN_PREFIX = 'WTD';

    public function getStateFormatAttr($value, $data)
    {
        $str = '';
        switch ($data['state'])
        {
            case 'waiting':
                $str = "审核中";
                break;
            case 'processing':
                $str = "处理中";
                break;
            case 'success':
                $str = "已成功";
                break;
            case 'error':
                $str = "异常";
                break;
        }
        return $str;
    }

    public function getWithdrawRespAttr($value, $data)
    {
        $str = '';
        if($data['state'] == 'success'){
            $str = "兑换成功";
        }elseif($data['state'] == 'error'){
            $str = $data['withdraw_resp'];
        }
        return $str;
    }

    public function createOrder($userId, $amount, $type)
    {
        $order_sn = date('Ymd') . substr((string)round(microtime(true) * 10000), 0, 23);
        // 第三方平台手续费1元
        $tax = 0;
        // 平台手续费百分比
        $rate        = Config::getConfigValue('app_rule_poundage') ?? 0;
        $bank = UserBank::where('user_id', $userId)->find();
        if(!$bank){
            ApiException('请先绑定银行卡信息');
        }
        $platformTax = $rate / 100 * $amount;
        return self::create([
            'user_id'        => $userId,
            'bank_id'        => $bank->id,
            'transfer_sn'    => self::TRANSFER_SN_PREFIX . $order_sn,
            'withdraw_sn'    => self::WITHDRAW_SN_PREFIX . $order_sn,
            'money'          => $amount,
            'transfer_money' => $amount + $tax,
            'withdraw_money' => $amount, // 手续费1元
            'tax'            => $tax,
            'platform_tax'   => $platformTax,
            'withdraw_resp'  => '[]',
            'state'          => 'waiting',
            'type'           => $type
        ]);
    }

    public function createAlipayOrder($userId, $amount, $type)
    {
        $alipay = User::where('id', $userId)->value('alipay');
        $order_sn = date('Ymd') . substr((string)round(microtime(true) * 10000), 0, 23);
        // 第三方平台手续费1元
        $tax = 0;
        // 平台手续费百分比
        $rate        = Config::getConfigValue('app_rule_poundage') ?? 0;
        $platformTax = $rate / 100 * $amount;
        return self::create([
            'user_id'        => $userId,
            'bank_id'        => '',
            'transfer_sn'    => self::TRANSFER_SN_PREFIX . $order_sn,
            'withdraw_sn'    => self::WITHDRAW_SN_PREFIX . $order_sn,
            'money'          => $amount,
            'transfer_money' => $amount + $tax,
            'withdraw_money' => $amount, // 手续费1元
            'tax'            => $tax,
            'platform_tax'   => $platformTax,
            'withdraw_resp'  => '[]',
            'state'          => 'waiting',
            'type'           => $type,
            'alipay'         => $alipay
        ]);
    }

    public function repairOrder($moneyLogId)
    {
        $moneyLog = UserMoneyLog::find($moneyLogId);
        $amount = -$moneyLog['amount'];
        $userId = $moneyLog['user_id'];
        $time = $moneyLog['create_time'];
        $order_sn = date('Ymd') . substr((string)round(microtime(true) * 10000), 0, 23);
        // 第三方平台手续费1元
        $tax = 0;
        // 平台手续费百分比
        $rate        = Config::getConfigValue('app_rule_poundage') ?? 0;
        $bank = UserBank::where('user_id', $userId)->find();
        if(!$bank){
            ApiException('请先绑定银行卡信息');
        }
        $platformTax = $rate / 100 * $amount;
        return self::create([
            'user_id'        => $userId,
            'bank_id'        => $bank->id,
            'transfer_sn'    => self::TRANSFER_SN_PREFIX . $order_sn,
            'withdraw_sn'    => self::WITHDRAW_SN_PREFIX . $order_sn,
            'money'          => $amount,
            'transfer_money' => $amount + $tax,
            'withdraw_money' => $amount, // 手续费1元
            'tax'            => $tax,
            'platform_tax'   => $platformTax,
            'withdraw_resp'  => '[]',
            'state'          => 'waiting',
            'create_time'    => $time,
            'update_time'    => $time
        ]);
    }
}